function pr(...arg){
	console.log(...arg);
}
$(function(){
	//formTags
	(function(){
		var matchScoreItem = $('.match-score-item');
		var questiontype = $('[name="args[questiontype]"]');
		var questiontypeV = questiontype.val();
		if( ['5','6'].indexOf(questiontypeV) != -1 ){
			//matchScoreItem.show();
		}
		questiontype.change(function(event) {
			if( ['5','6'].indexOf( $(this).val() ) != -1 ){
				//matchScoreItem.show();
			}else {
				//matchScoreItem.hide();
			}
		});

		matchScoreItem.on('focus', '[form-tags] [tag-input]', function(event) {
			$(this).parent().parent().attr('focus','');
		});
		matchScoreItem.on('blur', '[form-tags] [tag-input]', function(event) {
			$(this).parent().parent().removeAttr('focus');
		});

		matchScoreItem.on('click', '[form-tags]', function(event) {
			event.stopPropagation();
			$(this).children('[tag-input]').focus();
		});

		matchScoreItem.on('click', '[form-tags] [tag] i', function(event) {
			event.stopPropagation();
			$(this).parent().remove();
		});

		matchScoreItem.on('click', '[form-tags] [tag-input]', function(event) {
			event.stopPropagation();
	        if (event.which == 13) {
	        	handleV( $(this) );
	    		event.preventDefault();
	        }
		});

		matchScoreItem.on('blur', '[form-tags] [tag-input]', function(event) {
			event.stopPropagation();
			handleV( $(this) );
		});

		function handleV(that){
			var v = that.val();
	    	if( $.trim(v) != '' ){
	    		var hasValue = false;//判断值是否已经存在
	    		var tags = that.siblings('[tag]');
	    		$.each(tags, function(index, val) {
	    			if( $(this).children('input').val() == v ){
	    				hasValue = true;
	        			return false;
	    			}
	    		});
	    		var vStr = $('<div>').text(v).html();//转义html

	    		if( !hasValue ){
	    			var tagsName = that.parent().parent().attr('tags-name');
	    			// var itemList = that.closest('.input-list').children('.item');
	    			// var index = itemList.index( itemList.filter( that.closest('.item') ) );
	    			var index = that.closest('.item').attr('index');
	    			tagsName += '['+index+'][]';

	        		var htmlStr = '<p tag><input type="hidden" name="'+tagsName+'" value="'+v+'">'+vStr+'<i></i></p>';
	        		that.before(htmlStr);
	    		}
	    	}
			that.val('');
		}


		matchScoreItem.on('mousedown', '.del-item', function(event) {
			event.preventDefault();
		});

		matchScoreItem.on('click', '.del-item', function(event) {
			// event.stopPropagation();
			// event.preventDefault();
			$(this).parent().remove();
		});

		matchScoreItem.on('click', '.add-item', function(event) {
			event.stopPropagation();
			event.preventDefault();
			var formName = $(this).attr('form-name');
			var iptList = $(this).prev('.input-list');
			var index = parseInt(iptList.attr('curr-index'));
			iptList.attr('curr-index',index+1);

			var htmlStr = '<div class="item" index="'+(index+1)+'">\
								<div form-tags tags-name="'+formName+'">\
								  	<div mian>\
								  		<input tag-input type="text" placeholder="请在这里输入">\
								  	</div>\
								</div>\
								<span class="del-item glyphicon glyphicon-minus"></span>\
							</div>';
			$(this).prev().append(htmlStr);
		});
	}());
});